<?php
#基础函数
include_once('_path.php');

if (BENCHMARK) {
	require_once(_CONST_LOCAL_INCLUDE_PATH.'/Benchmark/Timer.php');
	$timer = new Benchmark_Timer();
	$timer->start();
}

#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

if (BENCHMARK) $timer->setMarker('init');

#用户信息
include_once(_CONST_LOCAL_INCLUDE_PATH."session.include.php");

if (BENCHMARK) $timer->setMarker('auth');

$token = $_POST['token'];
$act = $_POST['act'];
if( $act == 'register' ) {
	if( !empty($token) && ($token == $_SESSION['token'])) {
		$name = trim($_POST['username']);
		$pwd = trim($_POST['password']);
		$email = trim($_POST['email']);
		
		verify_field("username", "用户名", "empty");
		verify_field("username", "用户名", "minlength", 2);
		verify_field("username", "用户名", "maxlength", 14);
		verify_field("username", "用户名", "custom", "/^([\xa1-\xff0-9A-Za-z])+$/");
		verify_field("password", "密码", "empty");
		verify_field("password", "密码", "minlength", 6);
		verify_field("password", "密码", "maxlength", 14);
		verify_field("email", "邮箱", "empty");
		verify_field("email", "邮箱", "email");
		
		if (!count($error_strs)) {
			$database->execute("INSERT INTO user (name, pwd, email, lasttime, lastip) VALUES ("
			.string_sql($name)." ,".string_sql(md5($pwd))." ,".string_sql($email)." ,"
			.string_sql(_CONST_TIMENOW)." ,".string_sql(fetch_alt_ip())
			.");");
			if ($id = $database->getInsertId()) {
			    #写入扩展字段
				$database->execute("INSERT INTO user_info (user_id) VALUES (".$id.");");
				header("Location:" . _CONST_WEB_URL . 'login.php?username='.$name);
			}
		}
	}
}
#初始化第二步
$token = md5(uniqid().time());
$_SESSION['token'] = $token;

#cache lite
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite.php');
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite/Output.php');

$cache = new Cache_Lite_Output();

#用于meta的keywords标签
$keywords = "";
#用于meta的Description标签
$description = "";
#用于mete的http-equiv标签
$referrer = "";
#用于meta的title标签
$title = "注册 - Votings";
#用于获取针对本页的css样式
$css = array();
#用于获取针对本页的js
$js = array("jquery.validate.min");
#用于获取针对head的其他标签
$links = array();
#用于控制那个主菜单为
$o = "nologin";

$ads = array();

$post_code =<<<HTML
HTML;

if (BENCHMARK) $timer->setMarker('proc');

//if ($user['loggedin'] < 1)
//	cache_header(900);

//no_cache_header();

#header
include_once(_CONST_LOCAL_LAYOUT_PATH."_header.php");
?>
<script>
$(function(){
	jQuery.validator.addMethod("bug", function(value, element) {
    return this.optional(element) || /^[A-Za-z0-9\u4E00-\u9FA5]+$/.test(value);
}, "只允许使用中文字符、字母和数字！");
 
	var validator = $("#registerForm").validate({
	    errorElement:"span",
		rules: {
			username: {
				bug: true,
				required: true,
                rangelength: [2,14],
				remote: "ajax/user.php"
			},
			password: {
				required: true,
				rangelength: [6,14]
			},
			email: {
				required: true,
				email: true,
				remote: "ajax/email.php"
			}
		},
		messages: {
			username: {
				required: "请输入用户名！",
				rangelength:jQuery.format("只允许 {0} 至 {1} 个中文字符、字母和数字！"),
				remote: jQuery.format("此用户名已经存在！")
			},
			password: {
				required: "请输入密码！",
				rangelength:jQuery.format("只允许 {0} 至 {1} 个字符！")
			},
			email: {
				required: "请输入邮箱！",
				email: "不是有效的邮箱格式！",
				remote: jQuery.format("此邮箱已经存在！")
			}
		}
	});
});
</script>
<div id="main">
<form name="registerForm" id="registerForm" action="register.php" method="post">
<input name="token" id="token" type="hidden" value="<?php echo $token;?>">
<input name="act" id="act" type="hidden" value="register">
	<div class="box">
		<div class="box-header"><h1>注 册</h1></div>
		<div class="input-wrap clearfix">
			<label for="">用户名</label>
			<input type="text" id="username" name="username" class="login-user-name"  value="建议：使用你在网上常用昵称" onfocus = "this.value=''" onblur = "if(this.value==''){this.value='建议：使用你在网上常用昵称'}" />
			<?php echo show_error("username");?>
		</div>
		<div class="input-wrap clearfix">
			<label for="">邮箱</label>
			<input type="text" id="email" name="email" class="login-password" value="设置您的邮箱" onfocus = "this.value=''" onblur = "if(this.value==''){this.value='设置您的邮箱'}" />
		    <?php echo show_error("email");?>
		</div>
		<div class="input-wrap clearfix">
			<label for="">密码</label>
			<input type="password" id="password" name="password" class="login-password" value="输入您的密码" onfocus = "this.value=''" onblur = "if(this.value==''){this.value='输入您的密码'}" />
		    <?php echo show_error("password");?>
		</div>
		<input type="submit" name="login-btn" class="login-btn" value="" />
	</div>
</form>	
</div>
<!--main end-->
<?php
//footer
include_once (_CONST_LOCAL_LAYOUT_PATH."_footer.php");

if (BENCHMARK) $timer->setMarker('page');

if (BENCHMARK) {
	echo "<!-- init: ".$timer->timeElapsed('Start', 'init')." -->\n";
	echo "<!-- auth: ".$timer->timeElapsed('init', 'auth')." -->\n";
	echo "<!-- proc: ".$timer->timeElapsed('auth', 'proc')." -->\n";
	echo "<!-- page: ".$timer->timeElapsed('proc', 'page')." -->\n";
	echo "<!-- total: ".$timer->timeElapsed('Start', 'Stop')." -->\n";
}

?>
